package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    HttpServletRequest req;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "这是一个员工账号登录接口")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "这是一个员工账号登出接口")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 添加员工账号
     *
     * @param employeeDTO 前端传给后端的接受类
     * @return
     */
    @PostMapping
    @ApiOperation(value = "这是一个员工账号添加接口")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        Long currentId = BaseContext.getCurrentId();
        log.info("创建这个用户的账号id是{}",currentId);
        log.info("得到要新增的数据{}",employeeDTO.toString());
        boolean b = employeeService.save(employeeDTO);
        if (b) {
            return Result.success();
        }else {
            return Result.error("添加账户失败");
        }
    }

    /**
     * 员工查询的实现
     * @param employeePageQueryDTO 前端传给后端的接受类
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询，参数为：{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);//后续定义
        return Result.success(pageResult);
    }

    /**
     * 修改员工账号状态
     * @param status 状态码
     * @param id 员工id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("员工账号停用启用")
    public Result StarOrStop(@PathVariable Integer status,Long id) {
        log.info("账号id{},即将被修改状态码为{}",id,status);
        boolean b = employeeService.StarOrStop(status,id);
        if (b) {
            return Result.success();
        }else {
            return Result.error("修改账户失败");
        }
    }

    /**
     * 根据id查询员工信息
     * @param id 员工id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        log.info("查询到的数据是",employee);
        return Result.success(employee);
    }

    /**
     * 编辑员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("输入的修改信息为{}",employeeDTO);
        boolean b = employeeService.update(employeeDTO);
        if (b) {
            return Result.success();
        }else {
            return Result.error("修改账户失败");
        }
    }
}
